﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.App_Code;
using QXBook.Model;
using QXBook.Utils;
using QXBook.Controls.HtmlControls;
using QXBook.Service;
using System.Web.UI.HtmlControls;

namespace Web.SysManage.Orders
{
    public partial class OrdersList : AdminBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //检测管理员登录
            ServiceFactory.CreateInstance<AdminService>().CheckAdminLogin();
            ServiceFactory.CreateInstance<AdminService>().CheckAdminPop(30);

            lmc.EnableLinkNew = false;
            lmc.EnableLinkEdit = false;
            lmc.EnableLinkPublish = false;
            lmc.EnableLinkOn = false;
            lmc.EnableLinkOff = false;
            lmc.EnableLinkResave = false;
            lmc.EnableLinkDel = false;
            if (!Page.IsPostBack)
            {
                OrderCondition condition = this.GetPageCondition();
                this.TextBind(condition);
            }

            lmc.ShipmentHandler += new EventHandler(lmc_ShipmentHandler);

            lbtnAll.Click += new EventHandler(lbtnAll_Click);
            lbtnIsPay.Click += new EventHandler(lbtnIsPay_Click);
            lbtnIsShipments.Click += new EventHandler(lbtnIsShipments_Click);
            lbtnIsComplate.Click += new EventHandler(lbtnIsComplate_Click);
        }

        void lmc_ShipmentHandler(object sender, EventArgs e)
        {
            int flags = 0;
            int headerID = 0;
            foreach (RepeaterItem item in rptList.Items)
            {
                HtmlInputCheckBox CheckID = (HtmlInputCheckBox)item.FindControl("CheckID");
                if (CheckID.Checked)
                {
                    //ServiceFactory.CreateInstance<AdminRuleService>().r
                    HtmlInputHidden hidID = (HtmlInputHidden)item.FindControl("hidID");
                    headerID = CommonUtils.ToInt(hidID.Value);
                    flags++;
                }
            }

            if (flags == 1)
            {
                Response.Redirect("Shipment.aspx?HeaderID=" + headerID);
            }
            else if (flags > 1)
            {
                base.ResponseErch("只能选择一条");
            }
            else
            {
                base.ResponseErch("请选择一条订单");
            }
        }

        void lbtnAll_Click(object sender, EventArgs e)
        {
            hidIsPay.Value = string.Empty;
            hidIsShipments.Value = string.Empty;
            hidIsComplate.Value = string.Empty;

            OrderCondition condition = this.GetPageCondition();
            this.TextBind(condition);
        }

        void lbtnIsPay_Click(object sender, EventArgs e)
        {
            hidIsPay.Value = CommonUtils.ToInt(YesNo.Yes).ToString();
            hidIsShipments.Value = string.Empty;
            hidIsComplate.Value = string.Empty;

            OrderCondition condition = this.GetPageCondition();
            this.TextBind(condition);
        }

        /// <summary>
        /// 发货
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void lbtnIsShipments_Click(object sender, EventArgs e)
        {
            hidIsPay.Value = string.Empty;
            hidIsShipments.Value = CommonUtils.ToInt(YesNo.Yes).ToString();
            hidIsComplate.Value = string.Empty;

            OrderCondition condition = this.GetPageCondition();
            this.TextBind(condition);
        }

        void lbtnIsComplate_Click(object sender, EventArgs e)
        {
            hidIsPay.Value = string.Empty;
            hidIsShipments.Value = string.Empty;
            hidIsComplate.Value = CommonUtils.ToInt(YesNo.Yes).ToString();

            OrderCondition condition = this.GetPageCondition();
            this.TextBind(condition);
        }

        private OrderCondition GetPageCondition()
        {
            OrderCondition condition = new OrderCondition();
            if (!string.IsNullOrEmpty(hidIsPay.Value))
            {
                condition.IsPayroll = (YesNo)CommonUtils.ToInt(hidIsPay.Value);
            }

            if (!string.IsNullOrEmpty(hidIsShipments.Value))
            {
                condition.IsShipments = (YesNo)CommonUtils.ToInt(hidIsShipments.Value);
            }

            if (!string.IsNullOrEmpty(hidIsComplate.Value))
            {
                condition.IsComplate = (YesNo)CommonUtils.ToInt(hidIsComplate.Value);
            }

            return condition;
        }

        private void TextBind(OrderCondition condition)
        {
            PagerState ps = new PagerState();
            if (string.IsNullOrEmpty(CommonUtils.Get("page")))
            {
                ps.CurrentPage = 1;
            }
            else
            {
                ps.CurrentPage = CommonUtils.ToInt(CommonUtils.Get("page"));
            }
            ps.PageSize = 20;

            List<OrderHeaderInfo> list = ServiceFactory.CreateInstance<OrderService>().GetOrderList(ref ps, condition);

            this.rptList.DataSource = list;
            this.rptList.DataBind();
        }

        protected void rptList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            OrderHeaderInfo order = (OrderHeaderInfo)e.Item.DataItem;
            HtmlInputHidden hidID = (HtmlInputHidden)e.Item.FindControl("hidID");
            Anchor aOrderCode = (Anchor)e.Item.FindControl("aOrderCode");
            Span sConsignee = (Span)e.Item.FindControl("sConsignee");
            Span sPayroll = (Span)e.Item.FindControl("sPayroll");
            Span sShipments = (Span)e.Item.FindControl("sShipments");
            Span sComplate = (Span)e.Item.FindControl("sComplate");
            Span sAddress = (Span)e.Item.FindControl("sAddress");
            Span sAddDate = (Span)e.Item.FindControl("sAddDate");

            hidID.Value = order.HeaderID.ToString();
            aOrderCode.Text = order.OrderCode;
            hidID.Value = order.HeaderID.ToString();
            hidID.Value = order.HeaderID.ToString();
            sConsignee.Text = order.Consignee;
            sPayroll.Text = order.IsPayroll == YesNo.Yes ? "<img src='../images/icon/deled.gif' />" : string.Empty;
            sShipments.Text = order.IsShipments == YesNo.Yes ? "<img src='../images/icon/deled.gif' />" : string.Empty;
            sComplate.Text = order.IsComplate == YesNo.Yes ? "<img src='../images/icon/deled.gif' />" : string.Empty;
            sAddress.Text = order.Address;
            sAddDate.Text = order.AddDate.ToString("yyyy-MM-dd");
        }
    }
}